package at.ac.lbg.media.vis.client.theme.manager
{
	import at.ac.lbg.media.vis.client.theme.view.Category;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	
	import org.springextensions.actionscript.utils.ArrayUtils;
	
	public class CategoryManager
	{
		public var selectedCatsDict:Dictionary = new Dictionary();
		
		public function CategoryManager()
		{
		}
		
		/**
		 * Fill the property iconIndex of all categories in this arraycollection.
		 * For this, the array is sorted by countArtworks. The PetalLibrary 
		 * is sorted by the complexity of the petals. So, the categories that are used
		 * most get the simplest petals.
		 * */
		public function addIconIndex(flatCats:ArrayCollection) :Dictionary {
			// make a copy before sorting, because original array needs to
			// stay in this order for the category display list.
			var iconCats:Array = ArrayUtils.clone(flatCats.source);
			iconCats.sortOn("countArtworks", Array.DESCENDING | Array.NUMERIC);

			for each (var mcat:Category in iconCats) {
				if (mcat.isCategory) {
					ArrayUtils.removeItem(iconCats, mcat);
				} 
			}
			
			var dict:Dictionary = new Dictionary();
			var i:int = 1;
			for each (var cat:Category in iconCats) { 
				cat.iconIndex = i;
				i++;
				dict[cat.id] = cat;
			}
			return dict;
		}
		
		public function handleSelectedCats(selectedCats:ArrayCollection):void {
			selectedCatsDict = new Dictionary();
			if (selectedCats != null) {
				for each (var catId:int in selectedCats) {
					selectedCatsDict[catId] = catId;
				}
			}
			Application.application.legend.invalidateList();
		}
		

	}
}